﻿using System;
using System.ComponentModel;
using System.Drawing;
using Neonode.Forms;
using Microsoft.Win32;
using System.Reflection;

namespace StopWatch
{
    class FrmTimer : CustomView
    {
        private System.Windows.Forms.Timer tmrCountDown;
        private DateTime StartDate;
        private TimeSpan Time = new TimeSpan(0);
        Player.CorePlayerClass AudioPlayer;

        int iHours = 0;
        int iMinutes = 0;
        int iSeconds = 0;
        bool bCountDown = false;

        public FrmTimer()
        {
            InitializeComponent();

            Utilities.CheckBgColor();
        }

        private void InitializeComponent()
        {
            this.tmrCountDown = new System.Windows.Forms.Timer();
            this.tmrCountDown.Tick += new EventHandler(this.timerCountDown_Tick);
            this.tmrCountDown.Interval = 100;
            this.tmrCountDown.Enabled = false;
            
            this.ToolsSweep.Enabled = true;
            this.CloseViewSweep.Enabled = true;
            this.ToolsSweep.Occurred += new System.ComponentModel.CancelEventHandler(this.ToolsSweep_Occurred);
            this.CloseViewSweep.Occurred += new CancelEventHandler(CloseViewSweep_Occurred);
            this.AcceptSweep.Occurred += new CancelEventHandler(AcceptSweep_Occurred);
            this.Paint += new EventHandler<PaintEventArgs>(frmTimer_Paint);
            this.Click += new EventHandler<System.Windows.Forms.MouseEventArgs>(frmTimer_Click);
            this.Title.Height = 0;
            this.Description.Text = "Select time to count down from";
            
            AudioPlayer = new Player.CorePlayerClass();
        }

        void frmTimer_Click(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (bCountDown == false)
            {

                if (e.X > 8 && e.X < 56 && e.Y > 45 && e.Y < 93)
                {
                    if (iHours == 23)
                    {
                        iHours = 0;
                    }
                    else
                    {
                        iHours++;
                    }
                }
                if (e.X > 64 && e.X < 112 && e.Y > 45 && e.Y < 93)
                {
                    if (iMinutes == 59)
                    {
                        iMinutes = 0;
                    }
                    else
                    {
                        iMinutes++;
                    }
                }

                if (e.X > 120 && e.X < 168 && e.Y > 45 && e.Y < 93)
                {
                    if (iSeconds == 59)
                    {
                        iSeconds = 0;
                    }
                    else
                    {
                        iSeconds++;
                    }
                }

                if (e.X > 8 && e.X < 56 && e.Y > 95 && e.Y < 143)
                {
                    if (iHours == 0)
                    {
                        iHours = 23;
                    }
                    else
                    {
                        iHours--;
                    }
                }
                if (e.X > 64 && e.X < 112 && e.Y > 95 && e.Y < 143)
                {
                    if (iMinutes == 0)
                    {
                        iMinutes = 59;
                    }
                    else
                    {
                        iMinutes--;
                    }
                }

                if (e.X > 120 && e.X < 168 && e.Y > 95 && e.Y < 143)
                {
                    if (iSeconds == 0)
                    {
                        iSeconds = 59;
                    }
                    else
                    {
                        iSeconds--;
                    }
                }

                if (iHours == 0 && iMinutes == 0 && iSeconds == 0)
                {
                    AcceptSweep.Enabled = false;
                    this.Description.Text = "Select time to count down from";
                }
                else
                {
                    AcceptSweep.Enabled = true;
                    this.Description.Text = "Select time to count down from\r\nAcceptSweep to start countdown";
                }
            }
            this.Refresh();        
        }

        void frmTimer_Paint(object sender, PaintEventArgs e)
        {
            Font font = new Font("Subway Ticker", 24f, FontStyle.Regular);
            Graphics graphics = e.Graphics;

            if (bCountDown == false)
            {
                Utilities.DrawAlphaImage(Assembly.GetExecutingAssembly(), e.Graphics, 8, 45, 48, 48, "StopWatch.Images.Plus.png");
                Utilities.DrawAlphaImage(Assembly.GetExecutingAssembly(), e.Graphics, 64, 45, 48, 48, "StopWatch.Images.Plus.png");
                Utilities.DrawAlphaImage(Assembly.GetExecutingAssembly(), e.Graphics, 120, 45, 48, 48, "StopWatch.Images.Plus.png");
                Utilities.DrawAlphaImage(Assembly.GetExecutingAssembly(), e.Graphics, 8, 95, 48, 48, "StopWatch.Images.Minus.png");
                Utilities.DrawAlphaImage(Assembly.GetExecutingAssembly(), e.Graphics, 64, 95, 48, 48, "StopWatch.Images.Minus.png");
                Utilities.DrawAlphaImage(Assembly.GetExecutingAssembly(), e.Graphics, 120, 95, 48, 48, "StopWatch.Images.Minus.png");
            }
            
            graphics.DrawImage(Utilities.background, 6, 7);
            graphics.DrawString(this.iHours.ToString("00") + ":" + this.iMinutes.ToString("00") + ":" + this.iSeconds.ToString("00"), font, new SolidBrush(System.Drawing.Color.Black), (float)12, (float)9);
 
        }

        void ToolsSweep_Occurred(object sender, CancelEventArgs e)
        {
            new FrmMenu().ShowDialog();
            Utilities.CheckBgColor();
        }

        void CloseViewSweep_Occurred(object sender, CancelEventArgs e)
        {
            if (bCountDown == true)
            {
                this.Description.Text = "Select time to count down from";
                bCountDown = false;
                AcceptSweep.Enabled = true;
                tmrCountDown.Enabled = false;

                if (iHours == 0 && iMinutes == 0 && iSeconds == 0)
                {
                    AcceptSweep.Enabled = false;
                }
                else
                {
                    AcceptSweep.Enabled = true;
                    this.Description.Text = "Select time to count down from\r\nAcceptSweep to start countdown";
                }

                if (AudioPlayer.Playing == true)
                {
                    AudioPlayer.Stop();
                }

                this.Refresh();
            }
            else
            {
                this.Close();
            }
        }

        void AcceptSweep_Occurred(object sender, CancelEventArgs e)
        {
            if (bCountDown == false)
            {
                this.AcceptSweep.Enabled = false;
                bCountDown = true;
                this.Description.Text = "Countdown timer running\r\nCloseSweep to stop";

                this.StartDate = DateTime.Now;
                StartDate = StartDate.AddHours((double)iHours);
                StartDate = StartDate.AddMinutes((double)iMinutes);
                StartDate = StartDate.AddSeconds((double)iSeconds);

                this.tmrCountDown.Enabled = true;
            }
            else
            {
                bCountDown = false;
                this.Refresh();
            }           
        }

        void timerCountDown_Tick(object sender, EventArgs e)
        {

            TimeSpan span = StartDate.Subtract(DateTime.Now);

            RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Herbie\StopWatch\");
            if (((int)key.GetValue("KeepAwake")) == 1)
            {
                Utilities.KeepAwake(0x2a);
            }
            key.Close();
            
            this.iHours = span.Hours;
            this.iMinutes = span.Minutes;
            this.iSeconds = span.Seconds;

            this.Refresh();

            if (iHours <= 0 && iMinutes <= 0 && iSeconds <= 0)
            {
                iHours = 0;
                iMinutes = 0;
                iSeconds = 0;
                tmrCountDown.Enabled = false;
                this.Refresh();

                string alarmFile = Assembly.GetExecutingAssembly().GetName().CodeBase;
                alarmFile = alarmFile.Substring(0, alarmFile.LastIndexOf(@"\") + 1) + "alarm.wav";

                this.Description.Text = "Countdown timer finished\r\nCloseSweep to stop";
                try
                {
                    AudioPlayer.Init((int)this.Handle);
                    AudioPlayer.Move(0, 0, 0, 0);
                    AudioPlayer.Open(alarmFile);
                    AudioPlayer.Play();
                }
                catch { }
            }
        }
    }
}
